﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PropertyManagement.Core;
using PropertyManagement.Core.Model;

namespace PropertyManagement.Modal {
    public partial class MaintainForm : Form {
        public Maintain CurrentMaintain { get; set; }
        private MaintainView currentView { get; set; }
        protected MaintainManage MaintainManage { get; private set; }
        protected HouseOwnerManage HouseOwnerManage { get; private set; }

        private IEnumerable<HouseOwner> allHouseOwners; 
        public MaintainForm( ) {
            InitializeComponent( );
            MaintainManage = new MaintainManage( );
            HouseOwnerManage = new HouseOwnerManage();
        }

        public event EventHandler OnCreated;

        public event EventHandler OnUpdated;

        public MaintainForm(MaintainView currentMaintain)
            : this( ) {
            BindUpdateMaintain(currentMaintain);
            this.Text = "修改维修记录";
        }

        private void BindUpdateMaintain(MaintainView currentMaintain) {
            currentView = currentMaintain;


            CurrentMaintain = MaintainManage.LoadByCode(currentMaintain.MaintainCode);


        }

        private void button2_Click(object sender, EventArgs e) {
            Close( );
        }

        private void button1_Click(object sender, EventArgs e) {
            if (CurrentMaintain != null) {
                if (CheckInput( )) {
                    CurrentMaintain.Status = comboBox1.SelectedItem.ToString() == "已修";
                    CurrentMaintain.RepaireWorker = textBox4.Text.Trim( );
                    CurrentMaintain.MaintainType = (MaintainType)comboBox1.SelectedIndex;
                    CurrentMaintain.MaintainFee = decimal.Parse(textBox5.Text.Trim( ));

                    CurrentMaintain.LastActionUserID = StaticProvider.Instance.CurrentLoginedUser.SystemUserID;
                    CurrentMaintain.LastUpdate = DateTime.Now;
                    CurrentMaintain.Remark = textBox7.Text.Trim( );
                    CurrentMaintain.RepaireDate = DateTime.Today;

                    MaintainManage.Update(CurrentMaintain);
                   if (OnUpdated != null) {
                       OnUpdated(this, EventArgs.Empty);
                   }
                }
            } else {
                if (CheckInput( )) {
                    var house = HouseOwnerManage.Load(textBox1.Text.Trim());
                    if (house == null) {
                        FormHelper.ShowErrorMsg("请输入正确的门牌号");
                    }
                    CurrentMaintain = new Maintain( );
                    CurrentMaintain.Status = comboBox1.SelectedItem.ToString() == "已修"; ;
                    CurrentMaintain.MaintainCode = textBox3.Text.Trim( );
                    CurrentMaintain.MaintainTopic = textBox6.Text.Trim( );
                    CurrentMaintain.Remark = textBox7.Text.Trim( );
                    CurrentMaintain.RepaireWorker = textBox4.Text.Trim( );
                    CurrentMaintain.MaintainFee = decimal.Parse(textBox5.Text.Trim( ));
                    CurrentMaintain.MaintainType = (MaintainType)comboBox1.SelectedIndex;
                    CurrentMaintain.ApplyDate = dateTimePicker1.Value.Date;
                    CurrentMaintain.HouseID = house.HouseOwnerID;
                    MaintainManage.Add(CurrentMaintain);
                    if (OnCreated != null) {
                        OnCreated(this, EventArgs.Empty);
                    }
                }

            }
            Close();
        }

        private bool CheckInput( ) {
            if (textBox1.Text.Trim( ).Length == 0) {
                FormHelper.ShowErrorMsg("请输入报修业主门牌号");
                return false;
            }
            if (textBox2.Text.Trim( ).Length == 0) {
                FormHelper.ShowErrorMsg("请输入报修业主姓名");
                return false;
            }
            var i = 0m;
            if (!decimal.TryParse(textBox5.Text.Trim( ), out i)) {
                FormHelper.ShowErrorMsg("维修费格式不正确");
                return false;
            }
            if (textBox6.Text.Trim( ).Length > 100 || textBox7.Text.Trim( ).Length > 500) {
                FormHelper.ShowErrorMsg("维修内容或备注超出限制字数");
                return false;
            }
            return true;
        }

        private void MaintainForm_Load(object sender, EventArgs e) {
            comboBox2.SelectedIndex = 0;
            comboBox1.SelectedIndex = 0;

            if (CurrentMaintain != null) {
                textBox1.Text = currentView.HouseCode;
                textBox2.Text = currentView.OwnerName;
                textBox3.Text = currentView.MaintainCode;
                dateTimePicker1.Value = currentView.ApplyDate;
                comboBox1.SelectedIndex = currentView.StatusName=="未修"?0:1;
                textBox4.Text = currentView.RepaireWorker;
                textBox5.Text = currentView.MaintainFee.ToString("#00.00");
                comboBox2.SelectedText = currentView.MaintainTypeName;
                textBox6.Text = currentView.MaintainTopic;
                textBox7.Text = currentView.Remark;

                textBox1.Enabled = false;
                textBox2.Enabled = false;

                dateTimePicker1.Enabled = false;
                comboBox2.Enabled = false;
                textBox6.Enabled = false;
            } else {
                textBox3.Text = "m" + DateTime.Now.Ticks;
            }
            textBox3.Enabled = false;
            textBox5.Text = "0.00";
            allHouseOwners = HouseOwnerManage.LoadAll();
            textBox1.AutoCompleteCustomSource = new AutoCompleteStringCollection( );
            textBox1.AutoCompleteCustomSource.AddRange(allHouseOwners.Select(n => n.HouseCode).ToArray( ));
        }

        private void textBox1_Leave(object sender, EventArgs e) {
            var owner = allHouseOwners.FirstOrDefault(h => textBox1.Text.Trim() == h.HouseCode);
            if (owner != null) {
                textBox2.Text = owner.OwnerName;
            }
            else {
                FormHelper.ShowErrorMsg("请输入正确的门牌号");
            }
        }
    }
}
